    //
    //  HomeScreen.m
    //  ZooNgonDoiOS
    //
    //  Created by Trung sound on 6/4/12.
    //  Copyright (c) 2012 Personal. All rights reserved.
    //

#import "HomeScreen.h"
#import "ZooLoginController.h"
#import "GetTopLikeSpotTask.h"
#import "GetNearBySpotTask.h"
#import "GetTopNewSpotTask.h"
#import "ZooSpotBean.h"
#import "MainScreen.h"

@implementation HomeScreen
@synthesize parent = _parent;

#define VIEW_HEADER_HEIGHT 48
#define ITEM_HEADER_WIDTH 80
#define VIEW_TABBAR_HEIGHT 42
#define ANIMATION_DURATION 0.5

- (id)initWithParent:(CGRect)frame parent:(MainScreen *)pMainScreen
{
    self = [super initWithFrame:frame];
    if (self) {
            // Initialization code
            //        _headerHomeScreenView = [[HeaderHomeScreenView alloc]initWithFrame:CGRectMake(0, 0, 320, 48)];
        
        _vTabbar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 42)];
        _btnNearBy = [[UIButton alloc]init];
        _btnFeatured = [[UIButton alloc]init];
        _btnTopNew = [[UIButton alloc]init];
        _lbNearBy = [[UILabel alloc]init];
        _lbFeatured = [[UILabel alloc]init];
        _lbTopNew = [[UILabel alloc]init];   
        _vUnderTabbar = [[UIView alloc]init];
        [_vUnderTabbar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_TABBAR]]];
        
        _btnSearch = [[UIButton alloc]initWithFrame:CGRectMake(10, 52, 298, 39)];
        _vIconSearch = [[UIImageView alloc]init ];
        _lbSearch = [[UILabel alloc]init ];
            //        _tfSearch.delegate =self;
        
        _vContent = [[UIView alloc]initWithFrame:CGRectMake(10, 52, 298, 325)];
            //        _featuredTableView = [[FeaturedTableView alloc]initWithFrame:CGRectMake(0, 0, 298, 275)];
            //        _featuredTableView.spotDetailDelegate = self;
        
        _footerHomeScreenView = [[FooterHomeScreenView alloc]initWithFrame:CGRectMake(0, 412 - 26, 320, 26)];
        _mainScreen = pMainScreen;
        _zooLocation = [[ZooLocation alloc]init];
//        locationManage = [[CLLocationManager alloc]init];
//        locationManage.delegate = self;
//        [locationManage startUpdatingLocation];
//        [self performSelector:@selector(stopUpdateLocation)];
//        currentLocation = [[CLLocation alloc]init];
        [self setupView];
    }
    return self;
}

-(void)setupView {
        // Draw view Tabbar
    _vTabbar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:TABBAR_BACKGROUND]];
    _btnNearBy.frame = CGRectMake(0, 0, self.bounds.size.width /3, VIEW_TABBAR_HEIGHT);
    [_btnNearBy setBackgroundColor:[UIColor clearColor]];
    [_btnNearBy addTarget:self action:@selector(btnNearByClicked) forControlEvents:UIControlEventTouchUpInside];
    
    _lbNearBy.frame = CGRectMake(5, 5, self.bounds.size.width /3 -5, 30);
    _lbNearBy.backgroundColor = [UIColor clearColor];
    [_lbNearBy setText:STRING_NEARBY];
    [_lbNearBy setFont:[UIFont fontWithName:APP_MAIN_FONT size:15]];
    [_lbNearBy setTextColor:[UIColor grayColor]];
    [_lbNearBy setTextAlignment:UITextAlignmentCenter];
    [_btnNearBy addSubview:_lbNearBy];
    
    _btnFeatured.frame = CGRectMake((self.bounds.size.width /3), 0, self.bounds.size.width /3, VIEW_TABBAR_HEIGHT);
    [_btnFeatured setBackgroundColor:[UIColor clearColor]];
    [_btnFeatured addTarget:self action:@selector(btnFeaturedClicked) forControlEvents:UIControlEventTouchUpInside];

    _lbFeatured.frame = CGRectMake(5, 5, self.bounds.size.width /3 -5, 30);
    _lbFeatured.backgroundColor = [UIColor clearColor];
    [_lbFeatured setText:STRING_FEATURED];
    [_lbFeatured setFont:[UIFont fontWithName:APP_MAIN_FONT size:15]];
    [_lbFeatured setTextColor:[UIColor whiteColor]];
    [_lbFeatured setTextAlignment:UITextAlignmentCenter];
    [_btnFeatured addSubview:_lbFeatured];
    
    _btnTopNew.frame = CGRectMake((self.bounds.size.width /3)*2, 0, self.bounds.size.width /3, VIEW_TABBAR_HEIGHT);
    [_btnTopNew setBackgroundColor:[UIColor clearColor]];
    [_btnTopNew addTarget:self action:@selector(_btnTopNewClicked) forControlEvents:    UIControlEventTouchUpInside];
    
    _lbTopNew.frame = CGRectMake(5, 5, self.bounds.size.width /3 -5, 30);
    _lbTopNew.backgroundColor = [UIColor clearColor];
    [_lbTopNew setText:STRING_TOPNEW];
    [_lbTopNew setFont:[UIFont fontWithName:APP_MAIN_FONT size:15]];
    [_lbTopNew setTextColor:[UIColor grayColor]];
    [_lbTopNew setTextAlignment:UITextAlignmentCenter];
    [_btnTopNew addSubview:_lbTopNew];
    
    _vUnderTabbar.frame = CGRectMake(self.bounds.size.width/3, VIEW_TABBAR_HEIGHT - 5, self.bounds.size.width/3, 5);
    
    [_vTabbar addSubview:_vUnderTabbar];
    [_vTabbar addSubview:_btnNearBy];
    [_vTabbar addSubview:_btnFeatured];
    [_vTabbar addSubview:_btnTopNew];
    
        // draw view search
    [_btnSearch setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_SEARCH]]];
    [_vIconSearch setImage:[UIImage imageNamed:IMG_BTN_SEARCH]];
    _vIconSearch.frame = CGRectMake(8, 8, 24, 24);
    [_btnSearch addSubview:_vIconSearch];
    _lbSearch.frame = CGRectMake(45, 8, 200, 24);
    [_lbSearch setText:STRING_SEARCH_HINT];
    [_lbSearch setTextColor:[UIColor grayColor]];
    [_lbSearch setBackgroundColor:[UIColor clearColor]];
        //    _tfSearch.autocorrectionType = UITextAutocorrectionTypeNo;
    [_btnSearch addSubview:_vIconSearch];
    [_btnSearch addSubview:_lbSearch];
    [_btnSearch addTarget:self action:@selector(btnSearchClicked) forControlEvents:UIControlEventTouchUpInside];
    
        // draw table view content
    [_vContent addSubview:_featuredTableView];
    
    _activitiIndicator = [[LoadingScreenView alloc]initWithFrame:CGRectMake(10, 150, 298, 275)];
    
        //    [self addSubview:_headerHomeScreenView];
    [self addSubview:_vTabbar];
//    [self addSubview:_btnSearch];
    [self addSubview:_vContent];
    [self addSubview:_footerHomeScreenView];
    [self addSubview:_activitiIndicator];
    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]];
    
    [self btnFeaturedClicked];
    
        //    [_headerHomeScreenView release];
    [_vTabbar release];
    [_vUnderTabbar release];
    [_btnNearBy release];
    [_btnFeatured release];
    [_btnTopNew release];
    [_lbNearBy release];
    [_lbFeatured release];
    [_lbTopNew release];
    [_footerHomeScreenView release];
}

-(void)stopUpdateLocation {
    [locationManage stopUpdatingLocation];
    locationManage.delegate = nil;
}

#pragma mark - Control button clicked
-(void) btnNearByClicked {
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        _vUnderTabbar.frame = CGRectMake(0, VIEW_TABBAR_HEIGHT - 5, self.bounds.size.width/3, 5);
        [_lbNearBy setTextColor:[UIColor whiteColor]];
        [_lbFeatured setTextColor:[UIColor grayColor]];
        [_lbTopNew setTextColor:[UIColor grayColor]];    
        [_vTabbar addSubview:_vUnderTabbar];
    }completion:nil];
    
    [_activitiIndicator startAnimating];
    _activitiIndicator.hidden = NO;
    
    GetNearBySpotTask *getNearBySpotTask = [[GetNearBySpotTask alloc]initWithParams:@"105.8401011" lat:@"21.0418241"];
    [getNearBySpotTask doExecute:self callback:@selector(dataCallback:)];
    
}

-(void) btnFeaturedClicked {
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        _vUnderTabbar.frame = CGRectMake(self.bounds.size.width/3, VIEW_TABBAR_HEIGHT - 5, self.bounds.size.width/3, 5);
        [_lbNearBy setTextColor:[UIColor grayColor]];
        [_lbFeatured setTextColor:[UIColor whiteColor]];
        [_lbTopNew setTextColor:[UIColor grayColor]];
        [_vTabbar addSubview:_vUnderTabbar];
    }completion:nil];
    
    [_activitiIndicator startAnimating];
    _activitiIndicator.hidden = NO;
    
    GetTopLikeSpotTask *topLikeSpotTask = [[GetTopLikeSpotTask alloc]initWithParams:10];
    [topLikeSpotTask doExecute:self callback:@selector(dataCallback:)];
    
    [_vContent addSubview:_featuredTableView];
    
}

-(void) _btnTopNewClicked {
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        _vUnderTabbar.frame = CGRectMake((self.bounds.size.width/3)*2, VIEW_TABBAR_HEIGHT - 5, self.bounds.size.width/3, 5);
        [_lbNearBy setTextColor:[UIColor grayColor]];
        [_lbFeatured setTextColor:[UIColor grayColor]];
        [_lbTopNew setTextColor:[UIColor whiteColor]];
        [_vTabbar addSubview:_vUnderTabbar];
    }completion:nil];
    
    [_activitiIndicator startAnimating];
    _activitiIndicator.hidden = NO;
    
    GetTopNewSpotTask *topNewSpotTask = [[GetTopNewSpotTask alloc]initWithParams:10];
    [topNewSpotTask doExecute:self callback:@selector(dataCallback:)];
    
    [_vContent addSubview:_featuredTableView];
    
}

    //-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    //    [_lbSearch resignFirstResponder];
    //    return 1;
    //}

-(void)dataCallback:(NSMutableArray *)pArray {
    int size = [pArray count];
    if (size == 0) {
        UIView *vNodata = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 298, 275)];
        UILabel *lbNodata = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 298, 275)];
        [lbNodata setText:@"NO DATA"];
        [lbNodata setBackgroundColor:[UIColor whiteColor]];
        [vNodata addSubview:lbNodata];
        [_vContent addSubview:vNodata];
    } else {
        
//        NSLog(@"sLocation:%@",_zooLocation.sLocation);
        NSLog(@"lat:%f long:%f", _zooLocation.cLatitude,_zooLocation.cLongitude);
        [_footerHomeScreenView setLocationLatitude:_zooLocation.cLatitude longitude:_zooLocation.cLongitude];
        _featuredTableView = [[FeaturedTableView alloc]initWithFrame:CGRectMake(0, 0, 298, 325) object:pArray locatin:_zooLocation];
        _featuredTableView.spotDetailDelegate = _mainScreen;
        [_vContent addSubview:_featuredTableView];
        [_featuredTableView release];
        _featuredTableView = nil;
        
    }
    [_activitiIndicator stopAnimating];
    _activitiIndicator.hidden = YES;
}

-(void)btnSearchClicked {
    NSLog(@"search");
}

    ///**
    // * Implement delegate
    // * View spot detail when select a spot
    // */
    //- (void)pushToSpotDetailScreen:(ZooSpotBean *)spot {
    //    HomeViewController *homeViewController = _parent;
    //    [homeViewController pushToDetailController:spot];
    //
    //}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
